package com.leyou.auth.config;

import com.leyou.auth.task.AppTokenHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 虎哥
 */
@Configuration
public class FeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor(AppTokenHolder tokenHolder, JwtProperties prop){
        return new AppTokenRequestInterceptor(tokenHolder, prop);
    }

    /**
     * 自定义一个类，实现RequestInterceptor，里面添加头信息
     */
    private class AppTokenRequestInterceptor implements RequestInterceptor{
        private AppTokenHolder tokenHolder;
        private JwtProperties prop;

        AppTokenRequestInterceptor(AppTokenHolder tokenHolder, JwtProperties prop) {
            this.tokenHolder = tokenHolder;
            this.prop = prop;
        }

        @Override
        public void apply(RequestTemplate template) {
            template.header(prop.getApp().getHeaderName(), tokenHolder.getToken());
        }
    }
}
